package com.example.eback.webSocket;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class PermissionUtils {

    public static void main(String[] args) {
        // MongoDB连接字符串
        String connectionString = "mongodb://139.155.97.192:27017";
        // 连接到MongoDB服务器
        MongoClientURI uri = new MongoClientURI(connectionString);
        try (MongoClient mongoClient = new MongoClient(uri)) {
            // 连接到数据库
            MongoDatabase database = mongoClient.getDatabase("mydatabase");
            // 获取集合（表）
            MongoCollection<Document> collection = database.getCollection("mycollection");

            // 创建一个新文档
            Document document = new Document();
            document.append("name", "John Doe");
            document.append("age", 30);
            document.append("city", "New York");
            document.append("city11", "New York");

            // 插入文档
            collection.insertOne(document);
            System.out.println("文档插入成功！");

            // 查询文档
            Document query = new Document("name", "John Doe");
            Document result = collection.find(query).first();
            System.out.println("查询结果：" + result.toJson());
        }
    }
}
